package main

import (
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	// 地址重写实例
	// http://127.0.0.1:8888/test?id=1  =》 http://127.0.0.1:8081/reverse/test?id=1

	rs1 := "http://127.0.0.1:8081/reverse"
	targetUrl , err := url.Parse(rs1)
	if err != nil {
		log.Fatal("err")
	}
	proxy := httputil.NewSingleHostReverseProxy(targetUrl)

	proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
		if err != nil{
			writer.WriteHeader(500)
			writer.Write([]byte(err.Error()))
		}
	}
	log.Println("Reverse proxy server serve at : 127.0.0.1:8888")
	if err := http.ListenAndServe(":8888",proxy);err != nil{
		log.Fatal("Start server failed,err:",err)
	}
}